package com.aruba.attendance.ui.dialog

import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.rememberSaveable

/**
 * Created by aruba on 2023/3/8.
 */
@Composable
fun rememberPictureDialogState(
    showPictureDialog: Boolean = false,
    path: String = ""
): PictureDialogState {
    return rememberSaveable(saver = PictureDialogState.Saver) {
        PictureDialogState(
            mutableStateOf(showPictureDialog),
            path
        )
    }
}

class PictureDialogState(
    val showPictureDialog: MutableState<Boolean>,
    var path: String,
) {
    fun show(path: String) {
        showPictureDialog.value = true
        this.path = path
    }

    fun dismiss() {
        showPictureDialog.value = false
        this.path = ""
    }

    companion object {
        val Saver: Saver<PictureDialogState, *> = listSaver(
            save = { listOf(it.showPictureDialog.value, it.path) },
            restore = {
                PictureDialogState(
                    showPictureDialog = mutableStateOf(it[0] as Boolean),
                    path = it[1] as String
                )
            }
        )
    }
}